﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Data.Common;
using MMS.Model;

namespace MMS.DAL
{
    /// <summary>
    /// 管理员的数据库相关操作
    /// </summary>
    public class AdminService
    {

        static DBHelper db = null;
        public AdminService()
        {
            db = new SqlServerDBHelper();
        }


        public AdminInfo Login(string name, string pass)
        {
            string sql = string.Format(@"select * from AdminInfo 
where AdminName='{0}' and AdminPass='{1}'", name, pass);
            Console.WriteLine(sql);

            AdminInfo admin = null;
            DbDataReader dr = db.GetReader(sql);
            if (dr.Read())//判断是否查询到数据
            {
                admin = new AdminInfo();
                admin.AdminId = Convert.ToInt32(dr["AdminId"]);
                admin.AdminName = dr["AdminName"].ToString();
                admin.AdminPass = dr["AdminPass"].ToString();
                admin.AdminRole = Convert.ToInt32(dr["AdminRole"]);
                admin.AdminStatus = Convert.ToInt32(dr["AdminStatus"]);
            }
            dr.Close();
            db.Close();

            return admin;
        }

        /// <summary>
        /// 数据层返回管理员列表
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<AdminInfo> GetAllAdmin(string name)
        {
            List<AdminInfo> list = new List<AdminInfo>();
            string sql = string.Format(@"select * from AdminInfo 
where AdminName like '%{0}%'", name);
            Console.WriteLine(sql);

            DbDataReader dr = db.GetReader(sql);
            while (dr.Read())//判断是否查询到数据
            {
                AdminInfo admin = new AdminInfo();
                admin.AdminId = Convert.ToInt32(dr["AdminId"]);
                admin.AdminName = dr["AdminName"].ToString();
                admin.AdminPass = dr["AdminPass"].ToString();
                admin.AdminRole = Convert.ToInt32(dr["AdminRole"]);
                admin.AdminStatus = Convert.ToInt32(dr["AdminStatus"]);
                list.Add(admin);
            }
            dr.Close();
            db.Close();
            return list;
        }
    }
}
